﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HslCommunication.Enthernet.Redis
{
	/// <summary>
	/// 提供了redis辅助类的一些方法
	/// </summary>
	// Token: 0x02000105 RID: 261
	public class RedisHelper
	{
		/// <summary>
		/// 将字符串数组打包成一个redis的报文信息
		/// </summary>
		/// <param name="commands">字节数据信息</param>
		/// <returns>结果报文信息</returns>
		// Token: 0x0600166D RID: 5741 RVA: 0x000733B8 File Offset: 0x000715B8
		public static byte[] PackStringCommand(string[] commands)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append('*');
			stringBuilder.Append(commands.Length.ToString());
			stringBuilder.Append("\r\n");
			for (int i = 0; i < commands.Length; i++)
			{
				stringBuilder.Append('$');
				stringBuilder.Append(Encoding.UTF8.GetBytes(commands[i]).Length.ToString());
				stringBuilder.Append("\r\n");
				stringBuilder.Append(commands[i]);
				stringBuilder.Append("\r\n");
			}
			return Encoding.UTF8.GetBytes(stringBuilder.ToString());
		}

		/// <summary>
		/// 生成一个订阅多个主题的报文信息
		/// </summary>
		/// <param name="topics">多个的主题信息</param>
		/// <returns>结果报文信息</returns>
		// Token: 0x0600166E RID: 5742 RVA: 0x00073468 File Offset: 0x00071668
		public static byte[] PackSubscribeCommand(string[] topics)
		{
			List<string> list = new List<string>();
			list.Add("SUBSCRIBE");
			list.AddRange(topics);
			return RedisHelper.PackStringCommand(list.ToArray());
		}

		/// <summary>
		/// 生成一个取消订阅多个主题的报文信息
		/// </summary>
		/// <param name="topics">多个的主题信息</param>
		/// <returns>结果报文信息</returns>
		// Token: 0x0600166F RID: 5743 RVA: 0x000734A0 File Offset: 0x000716A0
		public static byte[] PackUnSubscribeCommand(string[] topics)
		{
			List<string> list = new List<string>();
			list.Add("UNSUBSCRIBE");
			list.AddRange(topics);
			return RedisHelper.PackStringCommand(list.ToArray());
		}

		/// <summary>
		/// 从原始的结果数据对象中提取出数字数据
		/// </summary>
		/// <param name="commandLine">原始的字节数据</param>
		/// <returns>带有结果对象的数据信息</returns>
		// Token: 0x06001670 RID: 5744 RVA: 0x000734D8 File Offset: 0x000716D8
		public static OperateResult<int> GetNumberFromCommandLine(byte[] commandLine)
		{
			OperateResult<int> result;
			try
			{
				string text = Encoding.UTF8.GetString(commandLine).TrimEnd(new char[]
				{
					'\r',
					'\n'
				});
				result = OperateResult.CreateSuccessResult<int>(Convert.ToInt32(text.Substring(1)));
			}
			catch (Exception ex)
			{
				result = new OperateResult<int>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 从原始的结果数据对象中提取出数字数据
		/// </summary>
		/// <param name="commandLine">原始的字节数据</param>
		/// <returns>带有结果对象的数据信息</returns>
		// Token: 0x06001671 RID: 5745 RVA: 0x00073540 File Offset: 0x00071740
		public static OperateResult<long> GetLongNumberFromCommandLine(byte[] commandLine)
		{
			OperateResult<long> result;
			try
			{
				string text = Encoding.UTF8.GetString(commandLine).TrimEnd(new char[]
				{
					'\r',
					'\n'
				});
				result = OperateResult.CreateSuccessResult<long>(Convert.ToInt64(text.Substring(1)));
			}
			catch (Exception ex)
			{
				result = new OperateResult<long>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 从结果的数据对象里提取字符串的信息
		/// </summary>
		/// <param name="commandLine">原始的字节数据</param>
		/// <returns>带有结果对象的数据信息</returns>
		// Token: 0x06001672 RID: 5746 RVA: 0x000735A8 File Offset: 0x000717A8
		public static OperateResult<string> GetStringFromCommandLine(byte[] commandLine)
		{
			OperateResult<string> result;
			try
			{
				bool flag = commandLine[0] != 36;
				if (flag)
				{
					result = new OperateResult<string>(Encoding.UTF8.GetString(commandLine));
				}
				else
				{
					int num = -1;
					int num2 = -1;
					for (int i = 0; i < commandLine.Length; i++)
					{
						bool flag2 = commandLine[i] == 13 || commandLine[i] == 10;
						if (flag2)
						{
							num = i;
						}
						bool flag3 = commandLine[i] == 10;
						if (flag3)
						{
							num2 = i;
							break;
						}
					}
					int num3 = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, 1, num - 1));
					bool flag4 = num3 < 0;
					if (flag4)
					{
						result = new OperateResult<string>("(nil) None Value");
					}
					else
					{
						result = OperateResult.CreateSuccessResult<string>(Encoding.UTF8.GetString(commandLine, num2 + 1, num3));
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 从redis的结果数据中分析出所有的字符串信息
		/// </summary>
		/// <param name="commandLine">结果数据</param>
		/// <returns>带有结果对象的数据信息</returns>
		// Token: 0x06001673 RID: 5747 RVA: 0x00073698 File Offset: 0x00071898
		public static OperateResult<string[]> GetStringsFromCommandLine(byte[] commandLine)
		{
			OperateResult<string[]> result;
			try
			{
				List<string> list = new List<string>();
				bool flag = commandLine[0] != 42;
				if (flag)
				{
					result = new OperateResult<string[]>(Encoding.UTF8.GetString(commandLine));
				}
				else
				{
					int num = 0;
					for (int i = 0; i < commandLine.Length; i++)
					{
						bool flag2 = commandLine[i] == 13 || commandLine[i] == 10;
						if (flag2)
						{
							num = i;
							break;
						}
					}
					int num2 = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, 1, num - 1));
					for (int j = 0; j < num2; j++)
					{
						int num3 = -1;
						for (int k = num; k < commandLine.Length; k++)
						{
							bool flag3 = commandLine[k] == 10;
							if (flag3)
							{
								num3 = k;
								break;
							}
						}
						num = num3 + 1;
						bool flag4 = commandLine[num] == 36;
						if (flag4)
						{
							int num4 = -1;
							for (int l = num; l < commandLine.Length; l++)
							{
								bool flag5 = commandLine[l] == 13 || commandLine[l] == 10;
								if (flag5)
								{
									num4 = l;
									break;
								}
							}
							int num5 = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, num + 1, num4 - num - 1));
							bool flag6 = num5 >= 0;
							if (flag6)
							{
								for (int m = num; m < commandLine.Length; m++)
								{
									bool flag7 = commandLine[m] == 10;
									if (flag7)
									{
										num3 = m;
										break;
									}
								}
								num = num3 + 1;
								list.Add(Encoding.UTF8.GetString(commandLine, num, num5));
								num += num5;
							}
							else
							{
								list.Add(null);
							}
						}
						else
						{
							int num6 = -1;
							for (int n = num; n < commandLine.Length; n++)
							{
								bool flag8 = commandLine[n] == 13 || commandLine[n] == 10;
								if (flag8)
								{
									num6 = n;
									break;
								}
							}
							list.Add(Encoding.UTF8.GetString(commandLine, num, num6 - num - 1));
						}
					}
					result = OperateResult.CreateSuccessResult<string[]>(list.ToArray());
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<string[]>(ex.Message);
			}
			return result;
		}
	}
}
